package com.anuo.app.common.persistence;


import com.anuo.app.common.entity.FilterBase;
import com.anuo.app.modules.user.entity.UserFilter;

import java.io.Serializable;
import java.util.List;

public class PageData<T> implements Serializable {
    private int totalRow;
    private List<T> entities;

    public PageData() {

    }

    public PageData(List<T> entities) {
        this.entities = entities;
    }

    public int getTotalRow() {
        return totalRow;
    }

    public void setTotalRow(int totalRow) {
        this.totalRow = totalRow;
    }

    public List<T> getEntities() {
        return entities;
    }

    public void setEntities(List<T> entities) {
        this.entities = entities;
    }


    //region 分页公共方法

    /**
     * 设置总行数
     *
     * @param pageData
     * @param filterBase
     * @param callback
     */
    public static void setTotalRow(PageData pageData, FilterBase filterBase, CallbackGetCount callback) {
        if (filterBase.getPageNumber() == 1) { //第一页才去获取总行数, 后续页不再获取, 牺牲一点实时性来获取更好的性能
            int totalRow = callback.getCount();
            pageData.setTotalRow(totalRow);
        }
    }

    /**
     * 设置分页列表
     *
     * @param pageData
     * @param list
     * @param <T>
     */
    public static <T> void setEntities(PageData pageData, List<T> list) {
        pageData.setEntities(list);
    }

    public interface CallbackGetCount {
        int getCount();
    }
    //endregion 分页公共方法


}
